package me.zhengjie.modules.system.domain

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.fasterxml.jackson.annotation.JsonTypeName
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.persistence.*
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.NotNull
import me.zhengjie.common.base.BaseEntity
import me.zhengjie.common.utils.JacksonUtil
import org.springframework.data.redis.core.RedisHash

/**
 * @author Kuki Wu
 * @date 2019-04-10
 */
@Entity
@Table(name = "sys_dict")
@JsonTypeName("sysdict")
@RedisHash("sysdict")
data class Dict (
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "dict_id")
    @Schema(description = "ID", hidden = true)
    var id: @NotNull(groups = [Update::class]) Long? = null,


    @OneToMany(mappedBy = "dict",fetch = FetchType.LAZY,  cascade = [CascadeType.PERSIST, CascadeType.REMOVE])
    @get:JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
    @get:JsonInclude(JsonInclude.Include.NON_EMPTY)
    var dictDetails: MutableList<DictDetail> = mutableListOf(),

    @Schema(description = "名称")
    var name: @NotBlank String? = null,

    @Schema(description = "描述")
    var description: String? = null

): BaseEntity(){
    annotation class Update

    companion object {
        fun fromJson(json: String): Dict? {
            val obj = JacksonUtil.parseObject(json, Dict::class.java)!!
            return when (obj) {
                is Dict -> obj
                else -> null
            }
        }
    }
    
    fun copy(source: Dict?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}